{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d66d67fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(x,a,b):\n",
    "    return a@x+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fbc34f4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d46f874",
   "metadata": {},
   "outputs": [],
   "source": [
    "i=0\n",
    "A1=np.array([[0,-2/3],\n",
    "            [-1/3,0]])\n",
    "B1=np.array([2,1/3])\n",
    "x=np.array([1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a09d0497",
   "metadata": {},
   "outputs": [],
   "source": [
    "xnew=func(x,A1,B1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e1401041",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.33333333, 0.        ])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xnew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e4d0af26",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=xnew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef2e7317",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.        , -0.11111111])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func(x,A1,B1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "15deed77",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.array([1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5b32d8dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8a24facd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [1.33333333 0.        ]\n",
      "1 [ 2.         -0.11111111]\n",
      "2 [ 2.07407407 -0.33333333]\n",
      "3 [ 2.22222222 -0.35802469]\n",
      "4 [ 2.23868313 -0.40740741]\n",
      "5 [ 2.27160494 -0.41289438]\n",
      "6 [ 2.27526292 -0.42386831]\n",
      "7 [ 2.28257888 -0.42508764]\n",
      "8 [ 2.28339176 -0.42752629]\n",
      "9 [ 2.28501753 -0.42779725]\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "for i in range(0,10):\n",
    "    xnew=func(x,A1,B1)\n",
    "    x=xnew\n",
    "    print(i,xnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5e17df5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.28571429, -0.42857143])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.inv([[3,2],[1,3]])@np.array([6,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "69a6f26c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a9fdfa47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for i in range(5,1,-1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2a9e1b66",
   "metadata": {},
   "outputs": [],
   "source": [
    "n=4\n",
    "#a=np.random.randn(n,n)\n",
    "#b=np.random.randn(n,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "14f4746e",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16796/2167009006.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "51bfb1a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "b=([-1.29751664,0.57871615,-0.31923584,-0.99643128])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2793ed3c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16796/538797688.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mD\u001b[0m\u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdiag\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "D= np.diag(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f7ae5938",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'D' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16796/3684568589.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mD\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdiag\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mD\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'D' is not defined"
     ]
    }
   ],
   "source": [
    "D=np.diag(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bd4843d7",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16796/2036444801.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtril\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "np.tril(a,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ac265f31",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16796/1884196270.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mL\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtril\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "L=np.tril(a,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "id": "48f14262",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.        ,  0.        ,  0.        ],\n",
       "       [-0.44131652,  0.        ,  0.        ,  0.        ],\n",
       "       [ 0.50247802,  0.63999909,  0.        ,  0.        ],\n",
       "       [-0.27167485,  1.16780284, -1.20845386,  0.        ]])"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "eea51d6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "U=np.triu(a,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "68774dda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.24194207,  0.36556387,  1.16877871],\n",
       "       [ 0.        ,  0.        ,  0.73412448,  0.58793628],\n",
       "       [ 0.        ,  0.        ,  0.        , -0.73480946],\n",
       "       [ 0.        ,  0.        ,  0.        ,  0.        ]])"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "ee0f82b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func1(x,E,F):\n",
    "    return E@x+F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "415b15d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "E= - np.linalg.inv(D)@(L+U)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "29357c1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        , -1.13546803, -1.71564245, -5.48524215],\n",
       "       [-1.6205226 ,  0.        ,  2.695719  ,  2.15891316],\n",
       "       [-6.38424351, -8.13151989,  0.        ,  9.33613482],\n",
       "       [-0.21073412,  0.90584722, -0.93737961,  0.        ]])"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "E"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "d2b1c37e",
   "metadata": {},
   "outputs": [],
   "source": [
    "F=  np.linalg.inv(D)@b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "87522710",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6.08942729, -2.12505666, -4.0560567 ,  0.77291686])"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "id": "77f8aad7",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.array([1,1,1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "c2dcc23c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 1])"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "9710bb19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [-14.42577992   1.1090529   -9.23568529   0.53065035]\n",
      "1 [ 5.58566669 -2.49893849 83.97757266 13.47489643]\n",
      "2 [-221.24052127  244.29431368  106.40730481  -81.38669417]\n",
      "3 [  -19.60897471   467.5375975  -1337.92391655   168.94513139]\n",
      "4 [  831.73069349 -3212.27732642 -2103.36433924  1682.56542438]\n",
      "5 [-1979.30884819 -3387.530028   36515.32729386 -1112.68277419]\n",
      "6 [-52703.56866241  99238.26579928  29789.94502974 -36879.42695176]\n",
      "7 [  38495.72515959   86091.04003796 -814800.87477586   73077.43351628]\n",
      "8 [  899299.84082554 -2101091.68515165  -263560.37273851   833651.46257263]\n",
      "9 [-1734888.42857899  -368041.43828699 19126798.05941682 -1845724.33489673]\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "for i in range(0,10):\n",
    "    xnew=func1(x,E,F)\n",
    "    x=xnew\n",
    "    print(i,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a449a5c5",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16796/1484316203.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mw\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1.01\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mE1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mw\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mL\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mD\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m@\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mL\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mD\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mw\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mU\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "w=1.01\n",
    "E1=np.linalg.inv(w*L+D)@((L-w)*D-w*U)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af3650a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "F1=np.linalg.inv(w*L+D)@(w*b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "056b321d",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'F1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_10652/1329931142.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mxnew\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfunc1\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mE1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mF1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mxnew\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'F1' is not defined"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "for i in range(0,10):\n",
    "    xnew=func1(x,E1,F1)\n",
    "    x=xnew\n",
    "    print(i,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "id": "5e1d43c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calstdError(xnew,x):\n",
    "    return np.sqrt(np.sum((xnew-x)**2/xnew.size))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "0fb1e0c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xnew.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26be1fad",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
